package com.sheng.whyclass.work.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.work.event.FindScoreEvent;

import de.greenrobot.event.EventBus;

public class StudentScoreActivity extends AppCompatActivity {


    private String contentName;
    private String id;
    private String w_c_id;

    private TextView title;
    private TextView content;
    private TextView score;




    public static void startStudentScoreActivity(Context context, String content, String id,String w_c_id) {
        Intent intent = new Intent(context, StudentScoreActivity.class);
        Bundle b = new Bundle();
        b.putString(Constant.KEY_TITLE, content);
        b.putString(Constant.CONTENT_ID, w_c_id);
        b.putString(Constant.KEY_CONTENT_ID, id);

        intent.putExtras(b);
        context.startActivity(intent);
    }

    private void perseIntent() {
        Intent intent = getIntent();
        if (intent == null || intent.getExtras() == null) {
            return;
        }

        Bundle b = intent.getExtras();
        contentName = b.getString(Constant.KEY_TITLE);
        id = b.getString(Constant.KEY_CONTENT_ID);
        w_c_id = b.getString(Constant.CONTENT_ID);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_score);
        perseIntent();
        initView();
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        content = (TextView) findViewById(R.id.content);
        score = (TextView) findViewById(R.id.score);
        content.setText(contentName);
        StringRequest request  = HttpProcessManager.getInstance().findWorkScore(Constant.HOST_WORK_SCORE, id,w_c_id);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);
    }

    public void onEventMainThread(FindScoreEvent event) {

        int code = event.code;
        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));
            return;
        } else {
            score.setText(event.data.getData().getScore());
            return;
        }

    }


}
